from typing import Optional, List, Union

from beanie import Document
from pydantic import Field, BaseModel
from datetime import datetime
from fastapi import Query

class WxFriendsFields(BaseModel):
    device_id: str = Field(..., description="主人的id")
    wx_id: str = Field(..., description="好友的wx_id")
    nickname: str = Field(..., description="昵称")
    remark: Union[str, dict] = ""
    tags: List[str] = Field(default=[], description="标签列表")
    desc: str = ""
    avatar: Optional[str] = Field(default=None, description="头像URL")
    created_at: datetime = Field(default_factory=lambda: datetime.now())
    updated_at: datetime = Field(default_factory=lambda: datetime.now())


class WxFriendsModel(Document, WxFriendsFields):
    """
    微信用户表
    """

    class Settings:
        name = "wx_friends"  # 集合名称
        use_state_management = True  # 可选：启用状态管理

    async def before_update(self):
        self.updated_at = datetime.now()

    def is_customer(self) -> bool:
        """
        判断是否是客户（remark字段是否以'客户'开头）
        :return: True 如果是客户，否则 False
        """
        return self.remark.startswith("客户")


class WxFriendsCreate(BaseModel):
    auth_key: str
    device_id: str

class WxId(BaseModel):
    wx_id: str
    remark: str

class FriendTagRequest(BaseModel):
    device_id: str
    wx_ids: List[str]
    tag: str

class MessageParams(BaseModel):
    msg: str
    auth_key: str
    to_users: List[str]
    type : int = 1



class TagRequest(BaseModel):
    tag: str

class FriendRemarkRequest(BaseModel):
    device_id: str
    wx_id: str
    remark: str


# class AuthCodeCreate(BaseModel, WxFriendsFields):
#     pass
#
# class AuthCodeOut(BaseModel, WxFriendsFields):
#     pass